package com.company.JUC.ThreadLocal;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author ylr
 * @version 1.0.0
 * @ClassName ThreadLocalTest.java
 * @Description TODO
 * @createTime 2021年12月07日 09:24:00
 */
public class ThreadLocalTest {
    public static final ThreadLocal threadLocal = new ThreadLocal();
    public static Integer  noSafeStr;

    public void test() {
        Integer key = (Integer) threadLocal.get();//null 10 11
        if (ObjectUtils.isEmpty(key)) {
            key = 10;
            if (ObjectUtils.isEmpty(noSafeStr)){
                noSafeStr = 100;
            }
            System.out.println(key+"  "+ noSafeStr);//10
            threadLocal.set(++key);//11
        }else {
            System.out.println(key +"  "+ noSafeStr++ );//11
            threadLocal.set(++key);//12
        }


    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                ThreadLocalTest threadLocalTest = new ThreadLocalTest();
                for (int j = 0; j <2; j++) {
                    threadLocalTest.test();
                }
            }).start();
        }
    }

}
